package wanyi.advancedSearch;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;

public class SearchTest {
	//
	private static IndexReader reader = null;
	static {
		try {
			reader.open(FileIndexUtils.getDirectory());
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//
	public IndexSearcher getIndexSearcher ()
	{
		try {
			if (reader == null)
			{
				reader = IndexReader.open(FileIndexUtils.getDirectory());
			}
			else
			{
				IndexReader newopenedReader = IndexReader.openIfChanged(reader);
				if (newopenedReader != null)
				{
					reader.close();
					reader = newopenedReader;
				}
			}
			return new IndexSearcher(reader);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//
	public void searchByQuery (Query query)
	{
		try {
			IndexSearcher searcher = getIndexSearcher();
			TopDocs tds = null;
			tds = searcher.search(query, 50);
			SimpleDateFormat sdf = new SimpleDateFormat ();
			for (ScoreDoc sd : tds.scoreDocs)
			{
				Document doc = searcher.doc(sd.doc);
				System.out.println ("sd.doc: " + sd.doc + " sd.score: " + sd.score +  "  " +doc.get("Filename") + "  "
						+ doc.get("Path") + "  " + doc.get("Size") + "  " + doc.get("Date") + "  Score: " + doc.get("Score"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	//
	public void searchBySort (String value, Sort sort)
	{
		try {
			IndexSearcher searcher = getIndexSearcher();
			QueryParser parser = new QueryParser(Version.LUCENE_35, "Content", new StandardAnalyzer(Version.LUCENE_35));
			Query query = parser.parse(value);
			TopDocs tds = null;
			if (sort != null)
			{
				tds = searcher.search(query, 50, sort);
			}
			else
			{
				tds = searcher.search(query, 50);
			}
			SimpleDateFormat sdf = new SimpleDateFormat ();
			for (ScoreDoc sd : tds.scoreDocs)
			{
				Document doc = searcher.doc(sd.doc);
				System.out.println ("sd.doc: " + sd.doc + " sd.score: " + sd.score +  "  " +doc.get("Filename") + "  "
						+ doc.get("Path") + "  " + doc.get("Size") + "  " + doc.get("Date") + "  Score: " + doc.get("Score"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//
	public void searchByFilter (String value, Filter filter)
	{
		try {
			IndexSearcher searcher = getIndexSearcher();
			QueryParser parser = new QueryParser(Version.LUCENE_35, "Content", new StandardAnalyzer(Version.LUCENE_35));
			Query query = parser.parse(value);
			TopDocs tds = null;
			if (filter != null)
			{
				tds = searcher.search(query, filter, 50);
			}
			else
			{
				tds = searcher.search(query, 50);
			}
			SimpleDateFormat sdf = new SimpleDateFormat ();
			for (ScoreDoc sd : tds.scoreDocs)
			{
				Document doc = searcher.doc(sd.doc);
				System.out.println ("sd.doc: " + sd.doc + " sd.score: " + sd.score +  "  " +doc.get("Filename") + "  "
						+ doc.get("Path") + "  " + doc.get("Size") + "  " + doc.get("Date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
